<?php 
@session_start();
include '../Security/CheckAdmin.php';
include '../Home/DefineVar.php';
include '../DataBase/ConnectDB.php';
 include '../Home/DefineVar.php';
 mysql_select_db(MYSQL_DATABASE,$link);

/**
 * ユーザの月単位での会議室使用料
 * @param $time1
 * @param $time2
 * @param $link
 * @param $accountName
 * @return unknown_type
 */
function FeeByMonth($time1,$time2,$link,$accountName){
	$arrayFee = array(0,0,0,0,0);
	
//	print($time1."/".$time2."<br>");
	//Tinh phi su dung va phat
	$result = mysql_query("SELECT orderstatus, fee FROM ".MYSQL_TABLE_ORDER." WHERE  accountname = '".$accountName."' AND orderstatus !='0' AND starttime >= '".$time1."' AND starttime < '".$time2."'",$link);
if(mysql_num_rows($result)!=0){
while($row=mysql_fetch_array($result)){
	if($row[0] ==1) $arrayFee[0] +=$row[1];
	if($row[0]==2) $arrayFee[1]+=$row[1];
}
}

//Tinh phi dat phong
$result = mysql_query("SELECT orderfee FROM ".MYSQL_TABLE_ORDER." WHERE accountname = '".$accountName."' AND orderstatus !='2' AND ordertime >= '".$time1."' AND ordertime < '".$time2."'",$link);
if(mysql_num_rows($result)!=0){
while($row=mysql_fetch_array($result)){
	$arrayFee[2] +=$row[0];
}
}

//Tinh phi cancel phong
$result = mysql_query("SELECT fee FROM ".MYSQL_TABLE_ORDER." WHERE accountname = '".$accountName."' AND  orderstatus ='0' AND canceltime >= '".$time1."' AND canceltime < '".$time2."'",$link);
if(mysql_num_rows($result)!=0){
while($row=mysql_fetch_array($result)){
	$arrayFee[3] +=$row[0];
}
}

$arrayFee[4]=$arrayFee[0]+$arrayFee[1]+$arrayFee[2]+$arrayFee[3];
return $arrayFee;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../FileForInterface/popcalendar.js"></script> 
<script type="text/javascript">
var d=new Date();
dtFormat = 'YYYY/MM';
//時間の検査
function chkDt(udt)
{
	if(udt.indexOf("/") == -1){
		alert('時間のフォーマットは違う '+dtFormat+'でした。');
		return false;
	}
	dt1 = udt.split("/")
	mm1 = parseInt(dt1[1]);
	yy1 = parseInt(dt1[0]);
	if(isNaN(mm1) || isNaN(yy1)){
		alert('入力時間は正しくないでした。');
		return false;
	}
	dt2 = new Date(mm1+'/'+'1'+'/'+yy1);
	mm2 = dt2.getMonth()+1;
	yy2 = dt2.getFullYear();
	
	//alert(dd1+'/'+mm1+'/'+yy1);
	//alert(dd2+'/'+mm2+'/'+yy2);
	if( mm1==mm2 && yy1==yy2)
		return true;
		//alert('Valid Date 2!');
	else{
		alert('入力時間は正しくないでした。');
		return false;
	}
		//alert('Invalid Date 2 !');
	//document.frm.mdt.focus();
}

	function validatorOnSubmit(){
		var startTime = document.forms[0].elements[1].value;
		var endTime = document.forms[0].elements[2].value;

		if(!chkDt(startTime))return false;
		if(!chkDt(endTime))return false;
		 return true;
	}
</script>
</head>
<body>
<form name="FeeInfor" action="FeeOfUser.php" method="GET" onSubmit="return validatorOnSubmit()" >

<h3>会議室使用料金の情報</h3>
<table border="1">
<tr><td>アカウント名</td><td><input type='text' name='accountName' <?php echo "value ='".$_GET['accountName']."'"; ?>></input></td></tr>
<tr><td>タイムから</td><td><input type ='text' id ='startDay' name='startDay' size = 10 <?php echo "value ='".$_GET['startDay']."'"; ?> ></input> 
<img src="../FileForInterface/show-calendar.gif" style="cursor:pointer"  onclick="popUpCalendar(this, document.getElementById('startDay'), 'yyyy/mm')" />
</td></tr>
<tr><td>タイムまで</td><td><input type ='text' id='endDay' name="endDay" size = 10 <?php echo "value ='".$_GET['endDay']."'"; ?>></input>
<img src="../FileForInterface/show-calendar.gif" style="cursor:pointer" onclick="popUpCalendar(this, document.getElementById('endDay'), 'yyyy/mm')" />
</td></tr>
</table>
<input type="submit" value="探索"></input>
</form>
<?php 
 //Phan trang va hien thi
$p = mysql_real_escape_string($_GET['p']);// currentPage
$accountName = mysql_real_escape_string($_GET['accountName']);
$StartDay=explode("/",$_GET["startDay"]);
$EndDay=explode("/",$_GET["endDay"]);
if(!$StartDay[1] || !$EndDay[1] || !$accountName){
	print("<br> <h1> アカウント名と時間を入力してください。</h1><br>");
	exit();
}
$query = "SELECT * FROM ".MYSQL_TABLE_USERINFOR." WHERE accountname='$accountName';";
     		$result = mysql_query($query, $link);
        if(mysql_num_rows($result)==0){
        	 print("<br><script type='text/javascript'> alert('".USER_ERR_FEE_MESS."'); window.location ='SearchUser.php';</script> ");
    		 exit();
        }
$StartTime=mktime(0,0,0,$StartDay[1],1,$StartDay[0]);
$EndTime=mktime(0,0,0,$EndDay[1],1,$EndDay[0]);

if((($EndTime - $StartTime)<0) ||(($EndTime - $StartTime)> MAX_MONTH_NUMBER *30*24*60*60)){
	print("<br> <h1> ".TIME_ERR_FEE_MESS."</h1><br>");
	exit();
}

//print("<h1> 会議室予約状態</h1>");
print("<table border ='2' bordercolor = '0000FF' cellspacing ='1' style='font-size: 18px'>
		<tbody align='center' size='5'><tr><td>年月</td><td>使用料</td><td>キャンセル料</td><td>罰金</td><td>予約手数料</td><td>総計</td></tr>");
do{
	$StartTime1=mktime(0,0,0,date('m',$StartTime)+1,1,date('Y',$StartTime));
	$arrayFee=FeeByMonth(date("Y-m-d G:i:s",$StartTime),date("Y-m-d G:i:s",$StartTime1),$link,$accountName);
	
	print("<tr><td>".date('Y/m',$StartTime)."</td><td>$arrayFee[0]</td><td>$arrayFee[3]</td><td>$arrayFee[1]</td>
				<td>$arrayFee[2]</td><td> $arrayFee[4]</td></tr>");
	
	$StartTime=$StartTime1;
}while($StartTime <= $EndTime);
print("</tbody></table>");

mysql_close($link);
?>
</body>
</html>